
/**
 * @author jiangqifan
 * @since 2016-5-26
 */

(function(){
  ADF.policy.DiagramXYLayoutPolicy = function(){
    ThisClass.superclass.constructor.call(this);
  }

  var ThisClass = ADF.policy.DiagramXYLayoutPolicy;

  //static properties
  COM.util.defineClass(ThisClass, COM.gef.XYLayoutEditPolicy, {
    createChangeConstraintCommand: function(request, child, rect){
      var model = child.getModel();
      if(rect.width < 30 || rect.height < 30) {
        return COM.gef.UnexecutableCommand.INSTANCE;
      }

      return COM.gef.Command.createCommand({
        label: 'change bounds',
        execute: function(){
          this.old = model.getLayoutData();
          model.setLayoutData(rect);
        },
        undo: function(){
          model.setLayoutData(this.old);
        }
      });

    },
    getCreateCommand: function(request){
      return COM.gef.Command.createCommand({
        label: 'create child',
        child: request.getNewObject(),
        parent: this.getHost().getModel(),
        execute: function(){
          //TODO 这里跟容器获取初始化的布局数据
          // var layoutdata = this.child.getLayoutData();
          var rect = new COM.draw2d.Rectangle();
          rect.width = 63;
          rect.height = 33;
          rect.setLocation(request.location);
          this.child.setLayoutData(rect);
          this.parent.addChild(this.child);
        },
        undo: function(){
          this.parent.removeChild(this.child);
        }
      });
    },
    createAddCommand: function(request, child, rect) {
      var command = new COM.gef.CompoundCommand(), deleteCommand = null, addCommand = null;
      deleteCommand = COM.gef.Command.createCommand({
        label: 'delete model',
        parentModel: child.getParent().getModel(),
        childModel:child.getModel(),
        oldBounds: child.getModel().getLayoutData(),
        execute: function(){
          this.parentModel.removeChild(this.childModel);
        },
        undo: function(){
          this.parentModel.addChild(this.childModel);
          this.childModel.setLayoutData(this.oldBounds);
        }
      });
      addCommand = COM.gef.Command.createCommand({
        label: 'add model',
        parentModel: this.getHost().getModel(),
        childModel:child.getModel(),
        execute: function(){
          this.parentModel.addChild(this.childModel);
          this.childModel.setLayoutData(rect);
        },
        undo: function(){
          this.parentModel.removeChild(this.childModel);
        }
      });
      command.add(deleteCommand);
      command.add(addCommand);
      return command.unwrap();
    },
    getConstraintFor: function(request, child) {
      var rectangle = new COM.draw2d.Rectangle(), childBoundsCopy = child.getModel().getLayoutData().copy(),
          childFigure = child.getFigure(), moveData = request.getMoveDelta();
      childFigure.translateToAbsolute(childBoundsCopy);
      var relativeLoaction = new COM.draw2d.Point(childBoundsCopy.x + moveData.x , childBoundsCopy.y + moveData.y);
      rectangle.setLocation(relativeLoaction);
      rectangle.setSize(child.getFigure().getLayoutData().getSize());
      return rectangle;
    }
    
  });

})();
